extends Weather


@onready var particles: GPUParticles2D = $GPUParticles2D
@onready var object_pool: ObjectPool = $ObjectPool
@onready var spawn_timer: Timer = $SpawnTimer

@export var map_material: Material
@export var once_count: int = 1    ## 一次产生的雨滴数量，与spawn_timer的wait_time配合从而控制雨势的大小
@export var raindrop_degree: float = 135.0
@export var raindrop_speed_range: Vector2 = Vector2(200, 250)
@export var raindrop_distance_range: Vector2 = Vector2(80, 150)


var _map: Node
var _map_original_material: Material


func _ready() -> void:
    spawn_timer.timeout.connect(_spawn_raindrop)


func startup(env: WorldEnvironment, map: Node2D) -> void:
    env.adjustment_brightness = env_brightness
    _map = map
    _map_original_material = map.material
    map.material = map_material

    particles.emitting = true


func end() -> void:
    _map.material = _map_original_material
    particles.emitting = false


func _spawn_raindrop() -> void:
    var rect := Utils.get_visible_global_rect(self)
    rect = rect.grow_individual(raindrop_distance_range.y, raindrop_distance_range.y, raindrop_distance_range.y, 0)
    for i in range(once_count):
        var pos := Utils.random_point_from_rect(rect)
        var raindrop := object_pool.acquire() as Raindrop
        raindrop.setup(pos, Vector2.from_angle(deg_to_rad(raindrop_degree)), 
                        Utils.random_from_vec2(raindrop_speed_range),
                        Utils.random_from_vec2(raindrop_distance_range))
